let scrollTop
let scrollLeft
let id
let toTop = document.querySelector('.top')
//监听窗口滚动事件
window.onscroll = function () {
    scrollTop = document.documentElement.scrollTop
    console.log(scrollTop);
    if (scrollTop > 500) {
        toTop.style.display = 'block'
    } else {
        toTop.style.display = 'none'
    }
}

//回到顶端操作
toTop.onclick = function () {
    if (id) {
        clearInterval(id)
    }
    id = setInterval(function () {
        scrollTop = document.documentElement.scrollTop
        scrollTop -= 10
        if (scrollTop <= 0) {
            scrollTop = 0
            return
        }
        document.documentElement.scrollTop = scrollTop
    }, 10)
}